package com.sxfq.loansupermarket.backsystem.modular.arrival.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.dao.CustomerAppointmentDao;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerAppointmentInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchCustomerAppointmentInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CustomerAppointmentServiceImpl implements ICustomerAppointmentService {
    @Autowired
    private CustomerAppointmentDao dao;

    @Override
    public List<CustomerAppointmentInfo> selectPageListByQueryGetMap(Page<CustomerAppointmentInfo> page, SearchCustomerAppointmentInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
            if (searchInfo.getOneChannel().indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = searchInfo.getOneChannel().split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("oneChannel", " t.one_channel in ('" + oneChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
            if (searchInfo.getTwoChannel().indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = searchInfo.getTwoChannel().split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("twoChannel", " t.two_channel in ('" + twoChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
            if (searchInfo.getThrChannel().indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = searchInfo.getThrChannel().split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("thrChannel", " t.thr_channel in ('" + thrChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            if("1".equals(searchInfo.getCustomerStatus())){
                params.put("appointmentStatus"," and (arrival_status=1 or arrival_status=6) ");
            } else {
                params.put("appointmentStatus"," and (arrival_status=9 or arrival_status=10) ");
            }
        }
        params.put("tableName", tableHead +"customer_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    public int getQueryPagesCount(Page<CustomerAppointmentInfo> page, SearchCustomerAppointmentInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            if("1".equals(searchInfo.getCustomerStatus())){
                params.put("appointmentStatus"," and (arrival_status=1 or arrival_status=6) ");
            } else {
                params.put("appointmentStatus"," and (arrival_status=9 or arrival_status=10) ");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
            if (searchInfo.getOneChannel().indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = searchInfo.getOneChannel().split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("oneChannel", " t.one_channel in ('" + oneChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
            if (searchInfo.getTwoChannel().indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = searchInfo.getTwoChannel().split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("twoChannel", " t.two_channel in ('" + twoChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
            if (searchInfo.getThrChannel().indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = searchInfo.getThrChannel().split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("thrChannel", " t.thr_channel in ('" + thrChannel + "')");
            }
        }
        params.put("tableName", tableHead +"customer_info");
        return dao.getQueryPagesCount(page, params);
    }
}